package com.poc.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.cloud.task.launcher.TaskLaunchRequest;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Component
@EnableBinding(Source.class)
public class MessageSenderService {
    private static final Logger LOGGER = LoggerFactory.getLogger(MessageSenderService.class);

    @Autowired
    private Source source;

    public void sendMessage(String args) {
        LOGGER.info("send message: {}", args);
        Map<String, String> properties = new HashMap<>();
        String redUrl = "maven://com.poc:task-processor:jar:1.0";
        TaskLaunchRequest request = new TaskLaunchRequest(
                redUrl, Collections.singletonList(args), properties, null,
                "task-processor");
        source.output().send(new GenericMessage<>(request));
    }
}
